﻿namespace the_moving_box
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //设置范围的长和宽
            int Width = 100, Height = 50;            

            //设置背景颜色等
            Console.BackgroundColor = ConsoleColor.Yellow;
            Console.Clear();
            Console.ForegroundColor = ConsoleColor.Red;
         
            int x = 0; int y = 0;

            Console.Write("hello");
           
            while (true)
            {
                char direction = Console.ReadKey(true).KeyChar;
                //清空上一次界面，不然上次的画面仍会保留
                Console.Clear();

                //选择目前光标位置
                Console.SetCursorPosition(x, y);

                //switch语句移动方块
                switch (direction)
                {
                    case 'd':
                        if (x < Width)
                            x += 2;
                        break;
                    case 'a':
                        if (x >= 2)
                            x -= 2;
                        break;
                    case 'w':
                        if (y >= 2)
                            y -= 1;
                        break;
                    case 's':
                        if (y < Height)
                            y += 1;
                        break;
                    default:
                        Console.SetCursorPosition(20, 21);

                        Console.WriteLine("         please input a character like wasd");
                        break;

                }
                
                Console.SetCursorPosition(x, y);
                Console.Write("hello");


            }
        }
    }
}